<?php

namespace App\Http\Controllers\Backend\Setting;

use App\Events\SettingLogEvent;
use App\Http\Requests\Backend\Setting\SettingchannelRequest;
use App\Models\SettingChannel;
use App\Models\SettingLoan;
use App\Models\SettingLog;
use App\Http\Controllers\Controller;
use App\Models\SettingSource;

class SettingChannelController extends Controller
{
    use \App\Library\Traits\SettingLog;
    const SETTING_TABLE = "setting_channels";
    public function __construct()
    {
        $this->middleware('auth:backend');
        $this->user = auth('backend')->user();
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $setting = new SettingChannel();
        $setting->setConnection('backend');
        $info = $setting->orderBy('id','desc')
            ->select('id','channel_code','channel_name','period_range','is_show_plan_table')
            ->get();
        return success($info);

    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(SettingchannelRequest $request)
    {
        $channelData = $request->only(['channel_code','channel_name']);
        $result = SettingChannel::create($channelData);
        if($result){
            //记录日志
            $this->setting_log($this->user->id,self::SETTING_TABLE,$result->id,SettingLog::LOG_INSERT,$channelData);
            return stored('');
        }else{
            return failed('创建失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $setting = new SettingChannel();
        $setting->setConnection('backend');
        $info =  $setting->select('id','channel_code','channel_name','period_range','is_show_plan_table')->where('id',$id) -> first();
        return success($info);

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(SettingchannelRequest $request, $id)
    {
        //
        $info = $request->only(['channel_code','channel_name','is_show_plan_table','period_range']);
        //判断Setting_loans表的period_range是否设置
        $loanPeriodRange = SettingLoan::isSetPeriodRange();
        if(isset($info['period_range'])){
            $loanPeriodRange = json_decode($loanPeriodRange,true);
            $period_range = json_decode($info['period_range'],true);
            $array = array_diff($period_range,$loanPeriodRange);
            if(!empty($array)){
                return failed('期限设置范围应小于首页无资方期限范围');
            }
            sort($period_range);
            $info['period_range'] = json_encode($period_range);
        }
        if(isset($info['channel_code'])){
            $res = SettingChannel::updateChannelCode($id,$info['channel_code']);
            if(!$res){
                return respond([],'更新失败');
            }
        }
        SettingChannel::where('id',$id)->update($info);
        $this->setting_log($this->user->id,self::SETTING_TABLE,$id,SettingLog::LOG_UPDATE,$info);
        return updated('');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $result = SettingChannel::destroy($id);
        if($result){
            $this->setting_log($this->user->id,self::SETTING_TABLE,$id,SettingLog::LOG_DELETE);
            return deleted();
        }else{
            return failed('删除失败');
        }
    }
}
